package com.technology.education.ui.student.evaluate

import android.view.Gravity
import android.widget.ImageView
import android.widget.TextView
import com.angcyo.dsladapter.dpi
import com.angcyo.dsladapter.dslItem
import com.google.android.flexbox.FlexboxLayout
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.TeacherOrderDetailModel
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.getColor
import com.technology.education.ext.getResId
import com.technology.education.ext.header
import com.technology.education.ext.toast
import me.hgj.jetpackmvvm.ext.parseState

/**
 * 评价完成跳转的结果页面
 */
class EvaluateResultFragment : BaseRecyclerTopBarFragment<EvaluateResultViewModel>() {
    override fun initViews() {
        setTitle("评价完成")
        initGrid(4)
        mViewModel.orderId=arguments?.getString("id")?:""
    }

    override fun lazyLoadData() {
        loading()
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            loadFinish()
            parseState(it, onSuccess = { it ->
                setUI(it)
            }, onError = { it -> toast(it.errorMsg) })
        }
    }

    private fun setUI(it: TeacherOrderDetailModel) {
        renderAdapter {
            dslItem(ItemSingleText(if (it.isSatisfied) "感谢您的评价,祝您学业有成" else "非常抱歉,给您带来不好的体验")) {
                style.itemSpan = -1
                style.titleTextSize = 18f
                style.isBold = true
                style.lineHeight = 0
                style.bottomPadding=8
            }
            if (!it.isSatisfied) {
                dslItem(ItemSingleText("我们将会对本次的辅导进行回看与审核,如果情况属实,本次订单将不收取费用。")) {
                    style.topPadding = 0
                    style.lineHeight=0
                    style.bottomPadding=0
                }
            }
            dslItem(ItemSingleText("辅导老师")) {
                style.itemSpan = -1
                style.titleTextSize = 18f
                style.isBold = true
                style.lineHeight = 0
            }
            dslItem(R.layout.item_evaluate_teacher_info) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemSpanCount = -1
                    itemHolder.tv(R.id.tv_name)?.text = it.teacher?.name
                    itemHolder.v<ImageView>(R.id.iv_header)?.apply {
                        header(it.teacher?.avatar, this)
                    }
                    itemHolder.v<ImageView>(R.id.iv_like)
                        ?.setBackgroundResource(if ((it.teacher?.isCollected == true)) R.drawable.ic_baseline_favorite_24 else R.drawable.ic_baseline_favorite_border_24)
                    itemHolder.click(R.id.iv_like) { _ ->
                        mViewModel.collectTeacher(
                            it.teacher?.id ?: "",
                            if (it.teacher?.isCollected == true) "0" else "1"
                        )
                        it.teacher?.collected = if (it.teacher?.isCollected == true) "0" else "1"
                        updateAdapterItem()
                    }
                }
            }
            dslItem(ItemSingleText("评价内容")) {
                style.itemSpan = -1
                style.titleTextSize = 18f
                style.isBold = true
                style.lineHeight = 0
            }

            dslItem(R.layout.item_evaluate_status_button) {
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemSpanCount = 2
                    itemHolder.tv(R.id.tv_status)?.apply {
                        background =
                            getResId(if (it.isSatisfied) R.drawable.bg_button else R.drawable.bg_round_gray)
                        setCompoundDrawablesWithIntrinsicBounds(
                            getResId(if (it.isSatisfied) R.drawable.ic_baseline_favorite_border_24 else R.drawable.ic_baseline_favorite_24),
                            null,
                            null,
                            null
                        )
                    }
                }
            }

            dslItem(R.layout.item_single_flexbox) {
                itemSpanCount = -1
                itemBindOverride = { itemHolder, _, _, _ ->
                    itemHolder.v<FlexboxLayout>(R.id.fl_content)?.apply {
                        removeAllViews()
                        it.commentTags?.forEach {
                            addView(TextView(requireContext()).apply {
                                text = it
                                setTextColor(getColor(R.color.text_m))
                                textSize = 14f
                                gravity = Gravity.CENTER
                                setPadding(8 * dpi, 4 * dpi, 8 * dpi, 4 * dpi)
                                background =
                                    getResId(R.drawable.round_search_history)
                            })
                        }
                    }
                }
            }

            it.question?.let {
                dslItem(ItemSingleText(it)) {
                    itemLeftInsert = 12 * dpi
                    itemRightInsert = 12 * dpi
                    style.lineColor = R.color.white
                    style.topPadding = 10
                    style.bottomPadding = 10
                    style.leftPadding = 12
                    style.rightPadding = 12
                    style.itemBackDrawable = R.drawable.round_gray
                    style.itemBackColor = 0
                    style.lineHeight = 0
                }
            }

        }

        setBottomButton("确定"){
            onBack()
        }
    }
}